/**
 * 录制日志,并将日志显示在一个新页面中
 */
export default {
  logs: [],
  isStart: false,
  consoleError: null,
  log(text) {
    const today = new Date();
    const hours = today.getHours();
    const minutes = today.getMinutes();
    const seconds = today.getSeconds();

    //编译后,自动添加堆栈没用
    this.logs.push(
      `${("0" + hours).slice(-2)}:${("0" + minutes).slice(-2)}:${(
        "0" + seconds
      ).slice(-2)} ${text}`
    );
  },
  startRecord() {
    this.isStart = true;
    this.logs = [];
    this.consoleError = console.error;
    console.error = (error) => {
      this.log(error);
    };
  },
  stopRecord() {
    this.isStart = false;
    console.error = this.consoleError;

    if (this.logs.length == 0) {
      return "<p>没有日志</p>";
    }
    let html = "<ul>";
    this.logs.forEach((e) => (html += `<li>${e}</li>`));
    html += "</ul>";

    this.newPage(html);
  },
  newPage(htmlString) {
    const newWindow = window.open("", "_blank");
    if (newWindow && newWindow.document) {
      newWindow.document.open();
      newWindow.document.write(htmlString);
      newWindow.document.close();
    } else {
      console.error("Failed to open a new page");
    }
  },
};
